﻿
var ObjBudgetCostUse = null;//选择合约规划类
var ObjStockCostUse = null; //选择公司库存类
var sProjType = "";          //合同类型
var sContractGUID = "";
var sApplyState="";

//功能：发起审批时校验,传递参数：付款申请GUID，申请金额、当月本合同可用金额
function CheckData(sHTFKApplyGUID, sApplyAmount, sMonthContractRemainAmount) {
    //首先通过通道获取参数
    GetDataFromXmlHttp(sHTFKApplyGUID);
	
	if(sApplyState != "未审核")
	{
	    alert(sApplyState + "的付款申请不允许发起审批！")
	    return false;
	}
	
	//公司库存的付款申请才需要				
	if(sProjType == "公司库存")
	{
	    if(CheckCostQK(sContractGUID,sHTFKApplyGUID,"发起审批") == false) 
        {
           return false;
        }
        
	    var sFile = "/Cbgl/HTFK/HTFK_XMLHTTP.aspx?HTFKApplyGUID=" + escape(sHTFKApplyGUID);
	    var sRtn = openXMLHTTP(sFile,"CheckProjectBudget");
	    if(sRtn != "OK"){
	        alert(sRtn.split("|")[1]);
	        return false;
	    }


	    //提交审批前校验所选合约规划是否被锁定
	    var strReturn = CheckBudgetLock(ObjBudgetCostUse);
	    if (strReturn != "OK") {
	        alert(strReturn.split("|")[1].split("，")[0] + "，不允许发起审批！");
	        return false;
	    }
	    
	    
     }
     
     if(!CheckHTFKApplyIfAmountEqual(ObjBudgetCostUse,ObjStockCostUse,sProjType,sContractGUID,sHTFKApplyGUID,stringToFloat(sApplyAmount),false)) return false;
     
     //校验当前工作流程与付款申请对应付款申请类型的工作流审批流程是否一至
     sFile = "/Cbgl/HTFK/HTFK_XMLHTTP.aspx?HTFKApplyGUID=" + escape(sHTFKApplyGUID);
     sRtn = openXMLHTTP(sFile,"getProcessGUID");
     var sUrl=window.parent.location.search;
     var sprocessGUID=sUrl.substr(sUrl.indexOf("processguid")+12,36);
     if(sprocessGUID.toLowerCase()!=sRtn.toLowerCase()){
        if (window.parent.appForm.ProcessModuleGUID.value.toLowerCase() != sRtn.toLowerCase()) //保存草稿时，要取父页面的appForm
        {
            alert("付款单据对应的流程模板不一致，不允许发起审批！");
            return false;
        }
     }
    
     //校验当前模板的月度资金计划，当月本合同可用金额是否与数据库里面的金额一至，不一至不允许发起                 
     sFile = "/Cbgl/HTFK/HTFK_XMLHTTP.aspx?HTFKApplyGUID=" + escape(sHTFKApplyGUID)+"&MonthContractRemainAmount=" + sMonthContractRemainAmount;
     sRtn = openXMLHTTP(sFile,"ValideMonthContractRemainAmount");
     if ("OK" != sRtn) {
         alert("当前表单的当月本合同可用金额与月度资金计划对应的金额不一致，不允许发起审批！\n请重新保存付款申请，并同步表单数据。");
         return false;
     }
	return true;
}

			
//提交审批前判断科目成本强控
function CheckCostQK(sContractGUID,sHTFKApplyGUID,sInfo)
{    	  
   //读取付款申请对应的合约规划使用信息
    var sRefGUID = sHTFKApplyGUID;
    var sRefType = "付款申请";
    var sCfSource = "合同";
    var sCfTypeName = "签约金额"; 
    
    if (sContractGUID == undefined || sContractGUID == "") 
    {
        alert("未读取到合同信息，请重试！");
        return false;
    }
	    
    ObjBudgetCostUse = new BudgetCostUse(sContractGUID,sRefGUID,sRefType,sCfSource,sCfTypeName,"付款申请");
    ObjStockCostUse = new StockCostUse(sContractGUID,sRefGUID,sRefType); 
    
    //校验选择的合约规划是否有效
    var sReturn = VerifyBudget(ObjBudgetCostUse);
    if(sReturn != "")
    {
        //重新刷新合约规划使用
        ObjBudgetCostUse = BudgetCostUseReload(ObjBudgetCostUse);
        //有无效合约规划使用
        // ChgUseCost(sReturn);
        return false;
     }
	        
	
    var strBudgetList=GetYjBudgetList(ObjBudgetCostUse.GetDataXml());//格式为合约规划GUID1;合约规划GUID2|对应金额1;对应金额2........  
	
    //判断强控
    if(!CheckCostCompel(strBudgetList,"付款申请",sHTFKApplyGUID,sInfo))
    {
        return false;
    }
    else
    {
        return true;
    }    
}

//后端获取数据，sProjType，sContractGUID，ObjBudgetCostUse，ObjStockCostUse
function GetDataFromXmlHttp(sHTFKApplyGUID)
{
    var sFile = "/Cbgl/HTFK/HTFK_XMLHTTP.aspx?HTFKApplyGUID=" + escape(sHTFKApplyGUID);

    var sRtn = openXMLHTTP(sFile,"GetHTRefApplyMsg");

    if ("FAIL" != sRtn)
    {                 
        //返回  ContractGUID|strProjType
        var sRtnList = sRtn.split("|");
        sContractGUID = sRtnList[0];
        sProjType = sRtnList[1];  
        sApplyState = sRtnList[2];  
        
        if(sProjType == "公司库存")
        {
            var sRefGUID = sHTFKApplyGUID;
            var sRefType = "付款申请";
            var sCfSource = "合同";
            var sCfTypeName = "签约金额";  
			    
            ObjBudgetCostUse = new BudgetCostUse(sContractGUID,sRefGUID,sRefType,sCfSource,sCfTypeName,"付款申请");
            ObjStockCostUse = new StockCostUse(sContractGUID,sRefGUID,sRefType);   
        }   
    }
}

//校验合约规划是否锁定
function CheckBudgetLock(ObjBudgetCostUse) {
    if (ObjBudgetCostUse) {
        if (ObjBudgetCostUse != "") {
            //判断合约规划是否锁定
            var sFile = "/Cbgl/HTDL/Contract_XMLHTTP.aspx?ProjGUIDList=" + ObjBudgetCostUse.ProjectGUID.join(",");
            var sRtn = openXMLHTTP(sFile, "CheckBudgetUse");
            return sRtn;
        }
        else {
            return "OK"
        }
    }
    else {
        return "OK"
    }
}